#include "SpriteDistance.h"
#include "CGlobal.h"
using namespace cocos2d;
SpriteDistance::SpriteDistance(void)
{
	fDistance_=0;
}

SpriteDistance::~SpriteDistance(void)
{
}

SpriteDistance * SpriteDistance::node()
{
	SpriteDistance *pNode=new SpriteDistance;
	if (pNode)
	{
		pNode->autorelease();
		if (pNode->init())
		{
			return pNode;
		}
	}
	return NULL;
}

bool SpriteDistance::init()
{
	if (!CCSprite::init())
	{
		return false;
	}
	if (!initText())
	{
		return false;
	}
	return true;
}

bool SpriteDistance::initText()
{
	CCLabelTTF*pText=CCLabelTTF::labelWithString("0m",CCSize(0,0), kCCTextAlignmentRight,"arial",20);
	if (pText)
	{
		addChild(pText,0,TAG_TEXT);
		
		return true;
	}
	return false;
}

void SpriteDistance::addDistance( float fAddDistance )
{
	fAddDistance=fAddDistance>0?fAddDistance:(-1*fAddDistance);
	fDistance_+=fAddDistance;
	updateText();
}

void SpriteDistance::updateText()
{
	char buf[100]={0};
	sprintf(buf,"%.2fm",fDistance_);
	CCLabelTTF *pText=(CCLabelTTF*) getChildByTag(TAG_TEXT);
	if (pText)
	{
		pText->setString(buf);
	}
}

float SpriteDistance::getDistance()
{
	return fDistance_;
}